//
//  BattleUILayer.m
//  VicBattle
//
//  Created by Nguyen Nguyen on 5/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VBBattleUILayer.h"

@implementation VBBattleUILayer

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    
    VBBattleUILayer *battleUILayer = [VBBattleUILayer node];
    [scene addChild:battleUILayer];
    
    return scene;
}
CGSize winSize;
-(id) init
{
    if((self=[super init]))
    {
        winSize = [[CCDirector sharedDirector] winSize];
        countmana = 0;
        
        battleLayer = [VBBattleLayer node];
        [self addChild:battleLayer];
        self.isTouchEnabled = YES;
        
        images = [NSArray arrayWithObjects:
                           @"archer.png",
                           @"knight.png",
                           @"ninja.png",
                           @"gunner.png",
                           @"healer.png",
                           @"soldier.png",
                           @"sorcerer.png",
                           nil];
        
        heroes = [[CCArray alloc] init];
        
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroes.plist"];

        int x = winSize.width;
        for(int i = images.count - 1; i >= 0 ; --i)
        {
            NSString *image = [images objectAtIndex:i];
            CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:image];
            x = x - 15 - sprite.textureRect.size.width;
            sprite.position = ccp(x, 30);
            sprite.tag = i;
            [self addChild:sprite];
            [heroes addObject:sprite];
        }
        
        item = [CCSprite node];
        item.visible = NO;
        [item setOpacity:128];
        [self addChild:item];
        
        [self schedule:@selector(gameLogic:) interval:5.0];
        
        label = [CCLabelTTF labelWithString:@"Mana" fontName:@"Marker Felt" fontSize:15];
        label.position = ccp(label.contentSize.width / 2 , winSize.height - label.contentSize.height / 2);
        static const ccColor3B ccVIOLET={238,130,238};
        [label setColor:ccVIOLET];
        [self addChild: label];
    }
    return self;
}

//mana
-(void) spriteMoveFinish: (id) sender
{
    CCSprite * sprite = (CCSprite *)sender;
    [self removeChild: sprite cleanup: YES];
}

-(id) addMana
{
    mana = [[CCSprite spriteWithFile:@"mana.png" rect:CGRectMake(0, 0, 48, 48)]retain];
    
    int minX = mana.contentSize.width / 2 + 100;
    int maxX = winSize.width - minX - 100;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;
    int actualY = winSize.height;
    
    mana.position = ccp(actualX, actualY);
    [self addChild: mana];
    
    int actualDuration = 4.0;
    
    id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(actualX, -mana.contentSize.height / 2)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinish:)];
    
    [mana runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];    
    return self;
}

-(void) gameLogic: (ccTime) dt
{
    [self addMana];
}

- (void)selectSpriteForTouch:(CGPoint)touchLocation
{
    for(CCSprite *sprite in heroes)
    {
        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) 
        {
            [item setTexture:sprite.texture];
            [item setTextureRect:sprite.textureRect];
            item.visible = YES;
            item.position = sprite.position;
            
            item.anchorPoint = ccp(item.anchorPoint.x, sprite.anchorPoint.y - 0.3);
            item.tag = sprite.tag;
            break;
        }
    }
}

CGPoint oldPoint, newPoint;

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    oldPoint = [touch locationInView:[touch view]];
    oldPoint = [[CCDirector sharedDirector] convertToGL:oldPoint];
    
    [self selectSpriteForTouch:oldPoint];
    
    if(CGRectContainsPoint(mana.boundingBox, oldPoint))
    {
        id actionMove = [CCMoveTo actionWithDuration:1.0 position:ccp(mana.contentSize.width / 2 , winSize.height - mana.contentSize.height / 2)];
        id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinish:)];
        [mana runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 
        
        countmana++;         
        label.string = [NSString stringWithFormat:@"%i", countmana];
    }
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    newPoint = [touch locationInView:[touch view]];
    newPoint = [[CCDirector sharedDirector] convertToGL:newPoint];
    
    if(item.visible == YES)
        item.position = newPoint;
    else
        [battleLayer viewWithOlPoint:oldPoint andNewPoint:newPoint];
    oldPoint = newPoint;
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(item.visible == YES)
    {
        int row, col;
        [battleLayer getRow:&row andCol:&col fromPoint:oldPoint];
        
        if(row != -1 && col != -1)
            [battleLayer putCharacter:item.tag toRow:row toCol:col];
    }
    item.visible = NO;
    
    //test touch for hpbar
    UITouch *touch = [touches anyObject];
    CGPoint touchnew = [touch locationInView:[touch view]];
    touchnew = [[CCDirector sharedDirector] convertToGL:touchnew];
    if(touchnew.x < 100)
    {
        if(battleLayer.vbChar.life > 0)
            battleLayer.vbChar.life -= 25;
        [battleLayer.vbChar.hpBar setPercentage:battleLayer.vbChar.life]; 
    } 
}

-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    item.visible = NO;
}

- (void) dealloc
{
    [heroes release];
    [mana release];
    [super dealloc];
}

@end
